package com.whfc.openapi.api.mach.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.fuum.dto.SysMachListDTO;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.mach.dto.*;
import com.whfc.openapi.api.mach.service.OpenApiMachService;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author sun_guodong
 * @description 开放平台-设备管理
 * @date 2020-06-22
 */
@RestController
@RequestMapping("/open/api/v1/mach")
public class OpenApiMachController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OpenApiMachService openApiMachService;

    @GetMapping("/type/list")
    public Result typeList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取设备类型列表,deptId:{}", deptId);
        ListData<SysMachListDTO> pageData = openApiMachService.typeList();
        return ResultUtil.success(pageData);
    }

    /**
     * 获取设备列表(分页)
     *
     * @param openApiClientDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取设备列表,deptId:{},pageNum:{},pageSize:{}", deptId, pageNum, pageSize);
        PageData<OpenApiMachDTO> pageData = openApiMachService.list(deptId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    /**
     * 获取设备列表(不分页)
     *
     * @return
     */
    @GetMapping("/list")
    public Result getMachList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        logger.info("获取首页设备列表。deptId:{}", openApiClientDTO.getDeptId());
        ListData<OpenApiMachDTO> listData = openApiMachService.list(openApiClientDTO.getDeptId());
        return ResultUtil.success(listData);
    }

    @GetMapping("/detail")
    public Result detail(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("machId") Integer machId) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取单台设备信息,deptId:{},machId:{}", deptId, machId);
        OpenApiMachDTO openApiMachDTO = openApiMachService.detail(machId);
        return ResultUtil.success(openApiMachDTO);
    }

    @GetMapping("/workHour")
    public Result workHour(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("开放平台查询设备每日工时,deptId:{},machId:{},date:{}", openApiClientDTO.getDeptId(), machId, date);
        AppMachWorkHourDayDTO dayDTO = openApiMachService.workHour(machId, date);
        return ResultUtil.success(dayDTO);
    }

    @GetMapping("/oilWear")
    public Result oilWear(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("开放平台查询设备每日油耗,deptId:{},machId:{},date:{}", openApiClientDTO.getDeptId(), machId, date);
        OpenApiOilWearDTO data = openApiMachService.oilWear(machId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/oilPos")
    public Result oilPos(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("开放平台查询设备每日油位信息,deptId:{},machId:{},date:{}", openApiClientDTO.getDeptId(), machId, date);
        ListData<Oil> listData = openApiMachService.oilPos(machId, date);
        return ResultUtil.success(listData);
    }

    @GetMapping("/gps")
    public Result gps(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("开放平台查询设备每日位置信息,deptId:{},machId:{},date:{}", openApiClientDTO.getDeptId(), machId, date);
        ListData<Gps> listData = openApiMachService.gps(machId, date);
        return ResultUtil.success(listData);
    }


}
